﻿using System;
using System.Data;
using System.Linq;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using GUI.Extensions;
using System.Collections.Generic;
using Utilities.Entities;

namespace GUI.Controls.News
{
    public partial class NewsZone : System.Web.UI.UserControl
    {
        public int ZoneId { get; set; }
        public bool DisplayPublishedDate = false;
        public bool GetDataFromNoCacheLayer = false;
        public int BoxId { get; set; }
        public string BoxTitle { get; set; }

        private const string LinkFormat = "<a href=\"{0}\" title=\"{2}\">{1}</a>";
        private const string RelateNewsFormat = "<li><a href=\"{0}\" title=\"{2}\">{1}</a></li>";
        public int Top = 2;
        public int TopOther = 3;



        protected void Page_Load(object sender, EventArgs e)
        {
            return;
            var bo = CacheSettingExtensions.EnableCacheHtml && GetDataFromNoCacheLayer ? NewsBO_NoCache.GetInstance() : NewsBO.GetInstance();

            if (ZoneId > 0)
            {
                var dt = bo.GetEmbedNewsOnPage(top: Top + TopOther, type: DefinedConstants.News.TypeIDs.EmbedNewsOnPage, zoneId: ZoneId);
                if (dt != null)
                {
                    var dtTemp = dt.Clone();
                    for (var i = 0; i < Math.Min(dt.Rows.Count, Top); i++)
                    {
                        dtTemp.ImportRow(dt.Rows[i]);
                    }
                    ctrNewsZoneContent.DataBind(dtTemp, DisplayPublishedDate, false);

                    var dtRelate = dt.Clone();
                    for (var i = TopOther - 1; i < Math.Min(dt.Rows.Count, Top + TopOther); i++)
                    {
                        dtRelate.ImportRow(dt.Rows[i]);
                    }
                    divRelateNews.Visible = BindRelateNews(dtRelate);

                    if (string.IsNullOrEmpty(BoxTitle))
                    {
                        var objCat = NewsBO.GetInstance().GetCategoryDetail(ZoneId);
                        BoxTitle = objCat == null
                                       ? string.Empty
                                       : string.Format(LinkFormat, objCat.Url, objCat.Name, PresentationUtils.EnCode(objCat.Name));
                    }
                    if (!string.IsNullOrEmpty(BoxTitle))
                    {
                        ltrHeader.Text = BoxTitle;
                    }
                    return;
                }
            }

            Visible = false;
        }

        private bool BindRelateNews(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();
                foreach (DataRow r in dt.Rows)
                {
                    sb.AppendFormat(RelateNewsFormat,
                                    r[SqlConstants.NewsPublish.Url],
                                    r[SqlConstants.NewsPublish.Title],
                                    r[SqlConstants.NewsPublish.EncodedTitle]);
                }
                ltrRelateNews.Text = sb.ToString();
                return true;
            }
            ltrRelateNews.Visible = false;
            return false;
        }

        public void DataBind(CategoryObject category, List<NewsObject> listNews, int imageWidth, int imageHeight)
        {
            if (category != null)
            {
                ltrHeader.Text = string.Format(LinkFormat, category.Url, category.Name,
                                               PresentationUtils.EnCode(category.Name));
            }
            if (listNews != null && listNews.Count > 0)
            {
                ctrNewsZoneContent.DataBind(listNews.Take(1).ToList(), imageWidth, imageHeight, false);
                BindRelateNews(listNews.Skip(1).Take(3));
            }
        }

        private void BindRelateNews(IEnumerable<NewsObject> enumerable)
        {
            var newsObjects = enumerable as NewsObject[] ?? enumerable.ToArray();
            if (enumerable != null && newsObjects.Any())
            {
                var sb = new StringBuilder();
                foreach (var r in newsObjects)
                {
                    sb.AppendFormat(RelateNewsFormat,
                                    r.Url,
                                    r.Title,
                                    PresentationUtils.EnCode(r.Title));
                }
                ltrRelateNews.Text = sb.ToString();
                return;
            }
            ltrRelateNews.Visible = false;

        }
    }
}